本文整理了Java中io.grpc.Attributes
类的一些代码示例,展示了Attributes
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Attributes
类的具体详情如下:
包路径:io.grpc.Attributes
类名称:Attributes
[英]An immutable type-safe container of attributes.
As a convention, annotations such as Grpc.TransportAttr is defined to associate attribute Keys and their propagation paths. The annotation may be applied to a Key definition field, a method that returns Attributes, or a variable of type Attributes, to indicate that the annotated Attributes objects may contain the annotated Key.
Javadoc users may click "USE" on the navigation bars of the annotation's javadoc page to view references of such annotation.
[中]属性的不可变类型安全容器。
####注释语义
按照惯例,注释(如Grpc)是可用的。TransportAttr定义为关联属性键及其传播路径。注释可应用于键定义字段、返回属性的方法或属性类型的变量,以指示注释属性对象可包含注释键。
Javadoc用户可以单击注释的Javadoc页面导航栏上的“使用”来查看此类注释的引用。
代码示例来源:origin: Netflix/concurrency-limits
/**
* Partition the limit by a request attribute.
* @return Chainable builder
*/
public GrpcServerLimiterBuilder partitionByAttribute(Attributes.Key<String> attribute) {
return partitionResolver(context -> context.getCall().getAttributes().get(attribute));
}
代码示例来源:origin: glowroot/glowroot
@Override
public void onAddresses(List<EquivalentAddressGroup> servers, Attributes attributes) {
synchronized (lock) {
if (closed) {
return;
}
this.servers.addAll(servers);
if (this.attributes == null) {
this.attributes = attributes;
} else if (!attributes.equals(this.attributes)) {
throw new IllegalStateException("New attributes \"" + attributes
+ "\" are not the same as existing attributes: " + this.attributes);
}
if (++onAddressesCount == nameResolvers.size()) {
Collections.shuffle(this.servers);
listener.onAddresses(this.servers, attributes);
close();
}
}
}
代码示例来源:origin: yidongnan/grpc-spring-boot-starter
@Nullable
@Override
public NameResolver newNameResolver(final URI targetUri, final Attributes params) {
final String clientName = targetUri.toString();
final GrpcChannelProperties clientConfig = this.config.getChannel(clientName);
URI remappedUri = clientConfig.getAddress();
if (remappedUri == null) {
remappedUri = this.defaultUriMapper.apply(clientName);
if (remappedUri == null) {
throw new IllegalStateException("No targetUri provided for '" + clientName + "'"
+ " and defaultUri mapper returned null.");
}
}
log.debug("Remapping target URI for {} to {} via {}", clientName, remappedUri, this.delegate);
final Attributes extendedParas = params.toBuilder()
.set(NameResolverConstants.PARAMS_CLIENT_NAME, clientName)
.set(NameResolverConstants.PARAMS_CLIENT_CONFIG, clientConfig)
.build();
return this.delegate.newNameResolver(remappedUri, extendedParas);
}
代码示例来源:origin: io.grpc/grpc-core
attributes.get(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG);
if (serviceConfig != null) {
String stickinessMetadataKey =
Attributes.Builder subchannelAttrs = Attributes.newBuilder()
代码示例来源:origin: io.grpc/grpc-core
@Override
public void handleResolvedAddressGroups(
List<EquivalentAddressGroup> servers, Attributes attributes) {
if (attributes.get(ATTR_LOAD_BALANCING_CONFIG) != null) {
throw new IllegalArgumentException(
"Unexpected ATTR_LOAD_BALANCING_CONFIG from upstream: "
+ attributes.get(ATTR_LOAD_BALANCING_CONFIG));
Map<String, Object> configMap = attributes.get(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG);
PolicySelection selection;
try {
ChannelLogLevel.DEBUG, "Load-balancing config: {0}", selection.config);
attributes =
attributes.toBuilder().set(ATTR_LOAD_BALANCING_CONFIG, selection.config).build();
代码示例来源:origin: venus-boot/saluki
private Attributes buildAttributes(GrpcURL subscribeUrl,
Map<List<SocketAddress>, GrpcURL> addressUrlMapping) {
Attributes.Builder builder = Attributes.newBuilder();
if (listener != null) {
builder.set(GrpcNameResolverProvider.NAMERESOVER_LISTENER, listener);
}
if (addresses.get(subscribeUrl) != null) {
builder.set(GrpcNameResolverProvider.REMOTE_ADDR_KEYS, addresses.get(subscribeUrl));
}
if (!addressUrlMapping.isEmpty()) {
builder.set(GrpcNameResolverProvider.GRPC_ADDRESS_GRPCURL_MAPPING, addressUrlMapping);
}
return builder.build();
}
代码示例来源:origin: io.grpc/grpc-core
/**
* Build the attributes.
*/
public Attributes build() {
if (newdata != null) {
for (Entry<Key<?>, Object> entry : base.data.entrySet()) {
if (!newdata.containsKey(entry.getKey())) {
newdata.put(entry.getKey(), entry.getValue());
}
}
base = new Attributes(newdata);
newdata = null;
}
return base;
}
}
代码示例来源:origin: io.grpc/grpc-core
sockaddrs.add(new InetSocketAddress(addr, record.port));
Attributes attrs = Attributes.newBuilder()
.set(GrpcAttributes.ATTR_LB_ADDR_AUTHORITY, authority)
.build();
代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded
/**
* Build the attributes.
*/
public Attributes build() {
if (newdata != null) {
for (Entry<Key<?>, Object> entry : base.data.entrySet()) {
if (!newdata.containsKey(entry.getKey())) {
newdata.put(entry.getKey(), entry.getValue());
}
}
base = new Attributes(newdata);
newdata = null;
}
return base;
}
}
代码示例来源:origin: yidongnan/grpc-spring-boot-starter
@Override
public Authentication readAuthentication(final ServerCall<?, ?> call, final Metadata metadata) {
final SSLSession sslSession = call.getAttributes().get(Grpc.TRANSPORT_ATTR_SSL_SESSION);
if (sslSession == null) {
log.trace("Peer not verified via SSL");
return null;
}
Certificate[] certs;
try {
certs = sslSession.getPeerCertificates();
} catch (final SSLPeerUnverifiedException e) {
log.trace("Peer not verified via certificate", e);
return null;
}
return fromCertificate(certs[certs.length - 1]);
}
代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded
sockaddrs.add(new InetSocketAddress(addr, port));
Attributes attrs = Attributes.newBuilder()
.set(GrpcAttributes.ATTR_LB_ADDR_AUTHORITY, srvHostname)
.build();
代码示例来源:origin: io.grpc/grpc-core
@Override
public boolean equals(Object o) {
if (!(o instanceof ClientTransportOptions)) {
return false;
}
ClientTransportOptions that = (ClientTransportOptions) o;
return this.authority.equals(that.authority)
&& this.eagAttributes.equals(that.eagAttributes)
&& Objects.equal(this.userAgent, that.userAgent)
&& Objects.equal(this.proxyParameters, that.proxyParameters);
}
}
代码示例来源:origin: net.devh/grpc-client-spring-boot-autoconfigure
@Nullable
@Override
public NameResolver newNameResolver(final URI targetUri, final Attributes params) {
final String clientName = targetUri.toString();
final GrpcChannelProperties clientConfig = this.config.getChannel(clientName);
URI remappedUri = clientConfig.getAddress();
if (remappedUri == null) {
remappedUri = this.defaultUriMapper.apply(clientName);
if (remappedUri == null) {
throw new IllegalStateException("No targetUri provided for '" + clientName + "'"
+ " and defaultUri mapper returned null.");
}
}
log.debug("Remapping target URI for {} to {} via {}", clientName, remappedUri, this.delegate);
final Attributes extendedParas = params.toBuilder()
.set(NameResolverConstants.PARAMS_CLIENT_NAME, clientName)
.set(NameResolverConstants.PARAMS_CLIENT_CONFIG, clientConfig)
.build();
return this.delegate.newNameResolver(remappedUri, extendedParas);
}
代码示例来源:origin: yidongnan/grpc-spring-boot-starter
/**
* Creates a new {@link NameResolver} for the given authority and attributes.
*
* @param targetAuthority The authority to connect to.
* @param params Optional parameters that customize the resolve process.
* @return The newly created name resolver for the given target.
*/
private NameResolver of(final String targetAuthority, final Attributes params) {
requireNonNull(targetAuthority, "targetAuthority");
// Determine target ips
final String[] hosts = PATTERN_COMMA.split(targetAuthority);
final List<SocketAddress> targets = new ArrayList<>(hosts.length);
for (final String host : hosts) {
final URI uri = URI.create("//" + host);
int port = uri.getPort();
if (port == -1) {
final Integer defaultPort = params.get(NameResolver.Factory.PARAMS_DEFAULT_PORT);
if (defaultPort == null) {
port = NameResolverConstants.DEFAULT_PORT;
} else {
port = defaultPort;
}
}
targets.add(new InetSocketAddress(uri.getHost(), port));
}
if (targets.isEmpty()) {
throw new IllegalArgumentException("Must have at least one target, but was: " + targetAuthority);
}
return new StaticNameResolver(targetAuthority, new EquivalentAddressGroup(targets));
}
代码示例来源:origin: io.grpc/grpc-grpclb
subchannel = subchannels.get(eag);
if (subchannel == null) {
Attributes subchannelAttrs = Attributes.newBuilder()
.set(STATE_INFO,
new AtomicReference<ConnectivityStateInfo>(
代码示例来源:origin: io.grpc/grpc-core
/**
* Returns true if the given object is also an {@link EquivalentAddressGroup} with an equal
* address list and equal attribute values.
*
* <p>Note that if the attributes include mutable values, it is possible for two objects to be
* considered equal at one point in time and not equal at another (due to concurrent mutation of
* attribute values).
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof EquivalentAddressGroup)) {
return false;
}
EquivalentAddressGroup that = (EquivalentAddressGroup) other;
if (addrs.size() != that.addrs.size()) {
return false;
}
// Avoids creating an iterator on the underlying array list.
for (int i = 0; i < addrs.size(); i++) {
if (!addrs.get(i).equals(that.addrs.get(i))) {
return false;
}
}
if (!attrs.equals(that.attrs)) {
return false;
}
return true;
}
代码示例来源:origin: venus-boot/saluki
public void refreshAffinity(Map<String, Object> affinity) {
Attributes nameresoveCache =
(Attributes) affinity.get(GrpcCallOptions.GRPC_NAMERESOVER_ATTRIBUTES);
this.current_server = (SocketAddress) affinity.get(GrpcCallOptions.GRPC_CURRENT_ADDR_KEY);
this.registry_servers = nameresoveCache.get(GrpcNameResolverProvider.REMOTE_ADDR_KEYS);
this.listener = nameresoveCache.get(GrpcNameResolverProvider.NAMERESOVER_LISTENER);
this.affinity = nameresoveCache;
}
代码示例来源:origin: io.grpc/grpc-grpclb
private LbAddressGroup flattenLbAddressGroups(List<LbAddressGroup> groupList) {
assert !groupList.isEmpty();
List<EquivalentAddressGroup> eags = new ArrayList<>(groupList.size());
String authority = groupList.get(0).getAuthority();
for (LbAddressGroup group : groupList) {
if (!authority.equals(group.getAuthority())) {
// TODO(ejona): Allow different authorities for different addresses. Requires support from
// Helper.
logger.log(ChannelLogLevel.WARNING,
"Multiple authorities found for LB. "
+ "Skipping addresses for {0} in preference to {1}", group.getAuthority(), authority);
} else {
eags.add(group.getAddresses());
}
}
// ALTS code can use the presence of ATTR_LB_ADDR_AUTHORITY to select ALTS instead of TLS, with
// Netty.
// TODO(ejona): The process here is a bit of a hack because ATTR_LB_ADDR_AUTHORITY isn't
// actually used in the normal case. https://github.com/grpc/grpc-java/issues/4618 should allow
// this to be more obvious.
Attributes attrs = Attributes.newBuilder()
.set(GrpcAttributes.ATTR_LB_ADDR_AUTHORITY, authority)
.build();
return new LbAddressGroup(flattenEquivalentAddressGroup(eags, attrs), authority);
}
代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded
/**
* Returns true if the given object is also an {@link EquivalentAddressGroup} with an equal
* address list and equal attribute values.
*
* <p>Note that if the attributes include mutable values, it is possible for two objects to be
* considered equal at one point in time and not equal at another (due to concurrent mutation of
* attribute values).
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof EquivalentAddressGroup)) {
return false;
}
EquivalentAddressGroup that = (EquivalentAddressGroup) other;
if (addrs.size() != that.addrs.size()) {
return false;
}
// Avoids creating an iterator on the underlying array list.
for (int i = 0; i < addrs.size(); i++) {
if (!addrs.get(i).equals(that.addrs.get(i))) {
return false;
}
}
if (!attrs.equals(that.attrs)) {
return false;
}
return true;
}
}
代码示例来源:origin: saturnism/grpc-java-by-example
@Override
public void applyRequestMetadata(MethodDescriptor<?, ?> methodDescriptor, Attributes attributes, Executor executor, MetadataApplier metadataApplier) {
String authority = attributes.get(ATTR_AUTHORITY);
System.out.println(authority);
executor.execute(new Runnable() {
@Override
public void run() {
try {
Metadata headers = new Metadata();
Metadata.Key<String> jwtKey = Metadata.Key.of("jwt", Metadata.ASCII_STRING_MARSHALLER);
headers.put(jwtKey, jwt);
metadataApplier.apply(headers);
} catch (Throwable e) {
metadataApplier.fail(Status.UNAUTHENTICATED.withCause(e));
}
}
});
}
内容来源于网络,如有侵权,请联系作者删除!