spring cloud --eureka --ribbon

Posted web-spring

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud --eureka --ribbon相关的知识,希望对你有一定的参考价值。

服务容器:

项目添加Eureka Server

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.RELEASE</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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

 

修改配置文件

spring:
  application:
    name: eureka-server
    
server:
  port: 2001
  
eureka:
  server:
    enable-self-preservation: false #eureka 的自我保护状态
  instance:
    hostname: eureka1 #集群通过hostname来区分
  client:
    register-with-eureka: false #不向自身注册
    fetch-registry: false #不从自身拉取注册信息
  

主程序入口修改

@EnableEurekaServer
@SpringBootApplication
public class SpEurekaApplication {

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

}

修改hosts文件,添加域名映射

127.0.0.1       eureka1
127.0.0.1       eureka2

服务提供:

  1.  添加eureka依赖

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

  2. 修改application.yml添加eureka注册配置

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka #为容器中的server.port的端口

  3. 为这个项目的主程序添加EnableDiscoveryClient注解

 

eureka的集群设置

在容器的application.yml中  ,使用不同的配置启动两个主程序

spring:
  profiles:
    active: eureka2
---
spring:
  profiles: eureka1
  application:
    name: eureka-server

server:
  port: 2001

eureka:
  instance:
    hostname: eureka1
  client:
    register-with-eureka: true  #profile的配置会覆盖公用配置
    fetch-registry: true        #profile的配置会覆盖公用配置
    service-url:
      defaultZone: http://eureka2:2002/eureka  #eureka1启动时向eureka2注册

---
spring:
  profiles: eureka2
  application:
    name: eureka-server

server:
  port: 2002
eureka:
  instance:
    hostname: eureka2
  client:
    register-with-eureka: true  #profile的配置会覆盖公用配置
    fetch-registry: true        #profile的配置会覆盖公用配置
    service-url:
      defaultZone: http://eureka2:2002/eureka  #eureka1启动时向eureka2注册

服务的提供者的application.yml文件修改

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

消费者:ribbon

添加EurekaDiscoveryClient依赖

application.yml文件

spring:
  application:
    name: ribbon
    
server:
  port: 3001
  
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

主程序修改

@EnableDiscoveryClient
@SpringBootApplication
public class SpRibbonApplication {
    
    //创建 RestTemplate 实例,并存入 spring 容器
  //@LoadBalanced //负载均衡注解
@Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpRibbonApplication.class, args); } }

使用时如在controller中:

@RestController
public class RibbonController {
    @Autowired
    private RestTemplate rt;
    
    @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) {
        //这里服务器路径用 service-id 代替,ribbon 会向服务的多台集群服务器分发请求
            //return rt.getForObject("http://localhost:8101/{1}", JsonResult.class, orderId);
        return rt.getForObject("http://item-service/{1}", JsonResult.class, orderId);
    }
}    

以上是关于spring cloud --eureka --ribbon的主要内容,如果未能解决你的问题,请参考以下文章

spring cloud eureka ip或者域名怎么获取

Spring Cloud:服务治理——Spring Cloud Eureka

基于Spring Cloud的微服务构建学习-3 Spring Cloud Eureka配置详解

spring cloud 使用Eureka作为服务注册中心

Spring Cloud微服务实战-服务治理(Spring Cloud Eureka)

Spring Cloud Eureka