[JAVA] Spring Cloud Netflix Note

Introduction

It is a memo when studying the contents written on the following site.

Spring Cloud Netflix

Eureka Client

Setting items

Basically, almost no settings are required. If spring-cloud-starter-netflix-eureka-client is included in the dependency in gradle etc. AutoConfigure works and sets it up

build.gradle(Excerpt from dependencies only)


dependencies {
	implementation('org.springframework.boot:spring-boot-starter-web')
	implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
	testImplementation('org.springframework.boot:spring-boot-starter-test')
}

Specify the Eureka server

application.yml


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Also, the application name is spring.application.name. Other than the above, basically default is OK If you want to change it, the default value is set below, so customization is required (specify in ʻeureka.instance. * `When setting in application.yml)

Eureka Server

Added spring-cloud-starter-netflix-eureka-server to dependencies AutoConfigure works by specifying @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

application.yml


server:
  port: 8761

When started, the management console is opened by accessing http: // localhost: 8761. I have a client that specifies this server in ʻeureka.client.serviceUrl.defaultZone` in application.yml Shown on the console if it is running

ass.JPG

Ribbon Client load balancer running over HTTP and TCP Used when the client calls the client, etc. Each Ribbon has a name and is set in RibbonClientConfiguration Ribbon allows you to specify the service name directly in the URL, such as http: // hoge-service / api. By introducing Ribbon, it is possible to describe the URL of the service name in RestTemplate, which is the REST API of Spring.

how to use

ʻOrg.springframework.cloud: spring-cloud-starter-netflix-ribbon` added to dependency

If you want to set RibbonClient, do as follows

@Configuration
@RibbonClient(name = "custom", configuration = CustomConfiguration.class )
public class RibbonConfiguration {

}

The contents of Custom Configuration will be overwritten with the contents set in RibbonClientConfiguration Other settings related to Ribbon can be overwritten by setting the following beans.

sample.jpg

↓ It looks like the following

@Configuration
protected static class FooConfiguration {
	@Bean
	public ZonePreferenceServerListFilter serverListFilter() {
		ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
		filter.setZone("myTestZone");
		return filter;
	}

	@Bean
	public IPing ribbonPing() {
		return new PingUrl();
	}
}

If you want to set default for all Ribbon Clients Use defaultConfiguration


@RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)
public class RibbonClientDefaultConfigurationTestsConfig {

	public static class BazServiceList extends ConfigurationBasedServerList {
		public BazServiceList(IClientConfig config) {
			super.initWithNiwsConfig(config);
		}
	}
}

@Configuration
class DefaultRibbonConfig {

	@Bean
	public IRule ribbonRule() {
		return new BestAvailableRule();
	}

	@Bean
	public IPing ribbonPing() {
		return new PingUrl();
	}

	@Bean
	public ServerList<Server> ribbonServerList(IClientConfig config) {
		return new RibbonClientDefaultConfigurationTestsConfig.BazServiceList(config);
	}

	@Bean
	public ServerListSubsetFilter serverListFilter() {
		ServerListSubsetFilter filter = new ServerListSubsetFilter();
		return filter;
	}

}

** Eureka manages Client information using Ribbon which overrides the above settings **

Zuul Zuul is a library positioned as an edge service that stands between each service and the client and acts like a gatekeeper of the system. There are various uses such as security check before calling a service, data conversion, single sign-on, URL rewriting, etc. Since it can be used as a reverse proxy role, by exposing only the service to which Zuul is applied to the outside You can prevent direct access to other services

How to use

ʻOrg.springframework.cloud: spring-cloud-starter-netflix-zuul` added to dependency Enabled by applying @EnableZuulProxy to the SpringBoot main class

@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class DemoApplication {

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

Can be routed with application.yml The following example specifies to forward requests coming in XXX / apples / ** to apple

application.yml


zuul:
  routes:
    apple: /apples/**

Reference site

Spring Cloud Netflix with Spring Boot 2.0 -Hystrix- Spring Special Study Group Report (Part 2)

Recommended Posts

Spring Cloud Netflix Note
Microservices in Spring Cloud
Spring Cloud Stream demo released
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Note
Note
Oauth2 authentication with Spring Cloud Gateway
Google Cloud Platform with Spring Boot 2.0.0
Spring Cloud Config Embedding the Config Server
Let's thoroughly explain Spring Cloud Gateway
Support Protocol Buffers for Spring Cloud Stream
Configure microservices with Spring Cloud (4): API Gateway