简介:。。。。。。。。
注册中心
Maven依赖信息
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--SpringCloud eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
application.yml 配置文件
###服务端口号 server: port: 7001 ###eureka 基本信息配置 eureka: instance: ###注册到eurekaip地址 hostname: localhost client: serviceUrl: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ ###因为自己是为注册中心,不需要自己注册自己 register-with-eureka: false ###因为自己是为注册中心,不需要检索服务 fetch-registry: false
启动Eureka服务
package net.riking.springcloud.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class AppEureka { public static void main(String[] args) { SpringApplication.run(AppEureka.class, args); } }
启动工程后,访问:http://localhost:7001/,可以看到下面的页面,其中还没有发现任何服务
注册服务提供者
Maven依赖信息
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot整合eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
application.yml 配置文件
#服务启动端口号 server: port: 8001 #服务名称(服务注册到eureka名称) spring: application: name: provider #客户端注册进eureka服务列表内 eureka: client: service-url: defaultZone: http://localhost:7001/eureka #该应用为注册中心,不会注册自己,默认true register-with-eureka: true #是否需要从eureka上获取注册信息,默认true fetch-registry: true
服务接口
package net.riking.springcloud.provider.controller; import net.riking.springcloud.provider.entity.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user/provider") public class UserProviderController { @GetMapping public User provider(@RequestParam String username) { User user = new User(); user.setUsername(username); user.setPassword("provider-eureka7001"); user.setDescription("注册服务提供者"); return user ; } }
启动provider服务
package net.riking.springcloud.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class AppUser { public static void main(String[] args) { SpringApplication.run(AppUser.class, args); } }
启动工程后,访问:http://localhost:7001/,可以看到下面的页面中提供者已经注册到eureka服务上
访问:http://localhost:8001/user/provider?username=提供者,可以看到下面的页面中信息输出
注册服务消费者
Maven依赖信息
与提供者服务相同
application.yml 配置文件
#服务启动端口号 server: port: 9001 #服务名称(服务注册到eureka名称) spring: application: name: consumer #客户端注册进eureka服务列表内 eureka: client: service-url: defaultZone: http://localhost:7001/eureka #该应用为注册中心,不会注册自己,默认true register-with-eureka: true #是否需要从eureka上获取注册信息,默认true fetch-registry: true
使用rest方式调用服务
package net.riking.springcloud.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/user/consumer") public class UserConsumerController { @Autowired private RestTemplate restTemplate; private static final String REST_URL_PREFIX = "http://PROVIDER"; @GetMapping public String consumer(@RequestParam String username) { String result = restTemplate.getForObject(REST_URL_PREFIX+"/user/provider?username="+username, String.class); return "消费者服务:"+result ; } }
启动消费者服务
package net.riking.springcloud.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class AppConsumer { public static void main(String[] args) { SpringApplication.run(AppConsumer.class, args); } @Bean @LoadBalanced //如果提供者服务为集群,当在请求时,拥有客户端负载均衡的能力,这里就不做演示 RestTemplate restTemplate() { return new RestTemplate(); } }
启动工程后,访问:http://localhost:7001/,可以看到下面的页面中消费者也注册到eureka服务上
访问:http://localhost:8001/user/consumer?username=消费者,可以看到下面的页面中信息输出