eureka注册器

首先创建一个eureka的模块,引入依赖,并且指定版本

<properties>
        <java.version>1.9</java.version>
        <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    </properties>





<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.配置文件

server:
  port: 10086
spring:
  application:
    name: itcast-eureka # 将来作为 微服务名称 注入要eureka容器,把自己注册给自己
eureka:
  client:
    service-url:
      defaultZone: http://localhost:${server.port}/eureka

3.启用注解,在引导类上使用

@EnableEurekaServer  // 启用eureka服务端
@SpringBootApplication
@EnableEurekaServer  // 启用eureka服务端
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

4.访问的时候

http://localhost:10086

在客户端中(就是服务提供方provider和调用方customer)

在provider中:

1.引入依赖,并且指定版本

//引入客户端依赖 
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>


 <properties>
        <java.version>1.9</java.version>
        // 指定版本
        <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    </properties>

//版本管理

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.配置文件application.yml中添加配置

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql:///eesy?serverTimezone=UTC
    username: root
    password: root
  application:
    name: itcast-provider  //配合 微服务名称
mybatis:
  type-aliases-package: cn.itcast.service.pojo

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka  // 注册到eureka中

3.在引导类中添加注解

@EnableDiscoveryClient // 启用client端
@SpringBootApplication
//@MapperScan("cn.itcast.service.pojo")
@EnableDiscoveryClient  //启用eureka-client服务
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }

}

就可以在eureka的访问页面中看到这个服务了

 

 

 

customer端一样的配置和provider一样的配置。

在引导类中除了添加  @EnableDiscoveryClient

还要在customer端的controller中注入DiscoveryClient 

但是要注意DiscoveryClient有两个bao

导入的包如下com.netflix.discovery,这个包是错误的,DiscoveryClient有两个包

应该导入import org.springframework.cloud.client.discovery.DiscoveryClient

 

@Controller
@RequestMapping("/customer/user")
public class UserController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/queryById")
    @ResponseBody
    private User queryById(@RequestParam(name = "id",required = true)int id){
        List<ServiceInstance> instances = discoveryClient.getInstances("ITCAST-PROVIDER");//获取指定服务名的提供的所有服务
        ServiceInstance serviceInstance = instances.get(0);
        return this.restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/queryById?id="+id,User.class);
    }
}

 

然后就可以了

 

posted @ 2020-05-03 01:23  刀锋意志1  阅读(185)  评论(0编辑  收藏  举报